package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    /**
     * 添加床位信息
     * @param bedDto
     * @return
     */
    @Override
    public void createBed(BedDto bedDto) {
        //1.转换为entity
        Bed bedInfo = BeanUtil.toBean(bedDto, Bed.class);
        //2.补全信息
        bedInfo.setBedStatus(0);
        bedInfo.setCreateTime(LocalDateTime.now());
        bedInfo.setUpdateTime(LocalDateTime.now());
        bedInfo.setCreateBy(1671403256519078138L);
        bedInfo.setUpdateBy(1671403256519078138L);
        //3.调用持久层
        log.info("补全信息{}",bedInfo);
        bedMapper.createBed(bedInfo);
    }

    /**
     * 根据ID回显床位信息
     * @param id
     * @return
     */
    @Override
    public BedVo getBedInfoByID(Integer id) {
        //查询信息
        Bed bedInfo = bedMapper.getBedInfoByID(id);
        //补充信息
        log.info("床位信息为{}",bedInfo);
        return BeanUtil.toBean(bedInfo,BedVo.class);
    }

    /**
     * 更新床位信息
     *
     * @param bedDto
     * @return
     */
    @Override
    public void updateBedInfo(BedDto bedDto) {
        //1.转换数据类型
        Bed updateBedInfo = BeanUtil.toBean(bedDto, Bed.class);
        //2.补全信息
        updateBedInfo.setCreateTime(LocalDateTime.now());
        updateBedInfo.setUpdateTime(LocalDateTime.now());
        updateBedInfo.setCreateBy(1671403256519078138L);
        updateBedInfo.setUpdateBy(1671403256519078138L);
        //3.调用mapper
        bedMapper.updateBedInfo(updateBedInfo);
    }

    /**
     * 根据床位ID删除床位信息
     * @param id
     * @return
     */
    @Override
    public void deleteBedInfoByID(Long id) {
        bedMapper.deleteBedInfoByID(id);
    }
}

